﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using MyWebAPI.Models;

#nullable disable

namespace MyWebAPI.Context
{
    public partial class MyContext : DbContext
    {
        public MyContext()
        {
        }

        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Course> Courses { get; set; }
        public virtual DbSet<Sc> Scs { get; set; }
        public virtual DbSet<Student> Students { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=.;User ID=sa;pwd=as;Initial Catalog=StuManager;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<Course>(entity =>
            {
                entity.HasKey(e => e.CourseNo)
                    .HasName("PK_Courses");

                entity.ToTable("Course");

                entity.Property(e => e.CourseNo)
                    .HasMaxLength(5)
                    .IsFixedLength(true);

                entity.Property(e => e.CourseName).HasMaxLength(20);
            });

            modelBuilder.Entity<Sc>(entity =>
            {
                entity.HasKey(e => new { e.StuNo, e.CourseNo })
                    .HasName("PK_Scs");

                entity.ToTable("Sc");

                entity.Property(e => e.StuNo)
                    .HasMaxLength(12)
                    .IsFixedLength(true);

                entity.Property(e => e.CourseNo)
                    .HasMaxLength(5)
                    .IsFixedLength(true);
            });

            modelBuilder.Entity<Student>(entity =>
            {
                entity.HasKey(e => e.StuNo)
                    .HasName("PK_Students");

                entity.ToTable("Student");

                entity.Property(e => e.StuNo)
                    .HasMaxLength(12)
                    .IsFixedLength(true);

                entity.Property(e => e.Birthday).HasDefaultValueSql("(getdate())");

                entity.Property(e => e.Gender)
                    .IsRequired()
                    .HasMaxLength(1)
                    .HasDefaultValueSql("(N'女')");

                entity.Property(e => e.Password).HasMaxLength(250);

                entity.Property(e => e.StuName).HasMaxLength(8);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
